package util;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Datum extends GregorianCalendar
{
/*Constructors*/
	public Datum(int dag,int maand, int jaar)
	{
		if(controleGeldigeDatum(jaar,maand,dag))
			super.set(jaar,(maand-1),dag);
	}
	public Datum()
	{
		super();
	}
	public Datum(java.sql.Date datum)
	{
		this(datum.getDay(),datum.getMonth(),datum.getYear());
	}
	
/*Methodes*/
	private boolean controleGeldigeDatum(int jaar, int maand, int dag) throws IllegalArgumentException
	{	
		SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
		s.setLenient(false);
		try {
			s.parse(String.format("%4d%02d%02d", jaar, maand, dag));
			return true;
		} 
		catch (ParseException e) {
			throw new IllegalArgumentException("Deze datum is niet geldig!");
		} 
	}
	public void controleActueleDatum()
	{
		if(((this.getTimeInMillis()/1000) - (new GregorianCalendar().getTimeInMillis()/1000))>=0);
		else
			throw new IllegalArgumentException("Deze datum is al voorbij!");
	}
	public int datumVerschil(Datum date)
	{
		return (int)((this.getTimeInMillis()/1000)-(date.getTimeInMillis()/1000))/(24*60*60);
	}
	public java.sql.Date converteerNaarSql()
	{
		java.util.Date sqlDatum = this.getTime();
		return new java.sql.Date(sqlDatum.getTime());
	}
	public String toString()
	{
		return String.format("%s/%s/%s",
    			this.getDag(),
    			this.getMaand(),
    			this.getJaar()
    			);
	}
	
/*Getters & setters*/
	public String getDag()
	{
		return String.format("%02d",this.get(Calendar.DAY_OF_MONTH));
	}
	public String getMaand()
	{
		return String.format("%02d",(this.get(Calendar.MONTH)+1));
	}
	public String getJaar()
	{
		return String.format("%4d",this.get(Calendar.YEAR));
	}
}
